home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2004 #9 / Amiga Plus CD - 2004 - No. 09.iso / amigaplus / tools / amigaos4_only / ripple / ripple.c < prev    next >
C/C++ Source or Header  |  2004-08-03  |  7KB  |  212 lines

  1. /* Ripple by Troels Walsted Hansen <troels@stud.cs.uit.no>, based on 
  2. ** xripple by Carsten «Rasterman» Haitzler <s2154962@cse.unw.edu.au>.
  3. **
  4. ** Source included for your enjoyment, feel free to modify at will.
  5. */
  6.  
  7. #include <proto/exec.h>
  8. #include <proto/dos.h>
  9. #include <proto/graphics.h>
  10. #include <proto/intuition.h>
  11. #include <math.h>
  12.  
  13. struct GfxBase *GfxBase = 0;
  14. struct IntuitionBase *IntuitionBase = 0;
  15. struct IntuitionIFace *IIntuition = 0;
  16. struct GraphicsIFace *IGraphics = 0;
  17. struct DOSIFace *IDOS = 0;
  18.  
  19. #ifndef PID2
  20. #define PID2 1.57079632679489661923
  21. #endif
  22.  
  23. #ifndef __AMIGADATE__
  24. #define __AMIGADATE__ "(7.11.97)"
  25. #endif
  26.  
  27. /* gcc has problems with inlining calls to library functions which take 
  28.    a lot of arguments in registers, therefore we use the amiga.lib stub 
  29.    function instead  */
  30.  
  31. #ifndef __amigaos4__
  32. #ifdef __GNUC__
  33. #undef BltBitMap 
  34. #endif
  35. #endif
  36.  
  37. #define TEMPLATE "HEIGHT/N,WAITTOF/S,NOWAIT/S,QUIET/S,USAGE/S"
  38. enum { A_HEIGHT, A_WAITTOF, A_NOWAIT, A_QUIET, A_USAGE, A_MAX};
  39.  
  40. static const char *version = "$VER: Ripple 1.0 " __AMIGADATE__;
  41.  
  42. int main(int argc, char **argv)
  43. {
  44.         int retval = RETURN_FAIL;
  45.         STRPTR error = NULL;
  46.  
  47.         struct RDArgs *rd;
  48.         LONG args[A_MAX] = {0};
  49.  
  50.         ULONG frames = 0, ticks;
  51.         struct DateStamp start_ds, end_ds;
  52.  
  53.         struct Screen *s = NULL;
  54.         struct Window *w = NULL;
  55.         struct BitMap *double_bm = NULL, *single_bm = NULL;
  56.         int screen_h, screen_w, screen_d, water_h;
  57.  
  58.         int y, yy, off, yoff, i = 0;
  59.         double a, incv = 0, inch = 0, aa, p;
  60.  
  61.         /* argument handling */
  62.  
  63.     GfxBase = OpenLibrary("graphics.library",0);
  64.     IGraphics = GetInterface(GfxBase,"main",1,0);
  65.     IntuitionBase = OpenLibrary("intuition.library",0);
  66.     IIntuition = GetInterface(IntuitionBase,"main",1,0);
  67.     IDOS = GetInterface(DOSBase,"main",1,0);
  68.  
  69.         rd = ReadArgs(TEMPLATE, args, NULL);
  70.         if(!rd)
  71.         {
  72.                 error = "Error in arguments";
  73.                 goto done;
  74.         }
  75.  
  76.         if(!args[A_QUIET])
  77.         {
  78.                 printf("%s by Troels Walsted Hansen <troels@stud.cs.uit.no>\n"
  79.                        "based on xripple by Carsten «Rasterman» Haitzler <s2154962@cse.unw.edu.au>\n",
  80.                        version+6);
  81.         }
  82.  
  83.         if(args[A_USAGE])
  84.         {
  85.                 error = "Usage: Ripple [<height>] [WAITTOF] [NOWAIT] [QUIET] [USAGE]\n"
  86.                         "Copyright © 1997 Ultima Thule Software";
  87.                 retval = RETURN_OK;
  88.                 goto done;
  89.         }
  90.  
  91.         /* initialization */
  92.  
  93.         s = LockPubScreen(NULL);
  94.         if(!s)
  95.         {
  96.                 error = "Couldn't lock default public screen";
  97.                 goto done;
  98.         }
  99.  
  100.         screen_h = s->Height;
  101.         screen_w = s->Width;
  102.         screen_d = GetBitMapAttr(s->RastPort.BitMap, BMA_DEPTH);
  103.         water_h  = args[A_HEIGHT] ? *(int *)args[A_HEIGHT] : 64;
  104.         if(!water_h || water_h > 32767) water_h = 64;
  105.  
  106.         w = OpenWindowTags(NULL, WA_Left,        0,
  107.                                  WA_Top,         screen_h - water_h,
  108.                                  WA_Height,      water_h,
  109.                                  WA_Width,       screen_w,
  110.                                  WA_SizeGadget,  FALSE, 
  111.                                  WA_DragBar,     FALSE, 
  112.                                  WA_CloseGadget, FALSE, 
  113.                                  WA_Borderless,  TRUE, 
  114.                                  WA_RMBTrap,     TRUE,
  115.                                  WA_PubScreen,   s,
  116.                                  TAG_DONE);
  117.         if(!w)
  118.         {
  119.                 error = "Couldn't open window";
  120.                 goto done;
  121.         }
  122.  
  123.         double_bm = AllocBitMap(screen_w, water_h * 2, screen_d, BMF_CLEAR | BMF_MINPLANES, s->RastPort.BitMap);
  124.         if(!double_bm)
  125.         {
  126.                 error = "Couldn't allocate double height bitmap";
  127.                 goto done;
  128.         }
  129.  
  130.         single_bm = AllocBitMap(screen_w, water_h, screen_d, BMF_CLEAR | BMF_MINPLANES, s->RastPort.BitMap);
  131.         if(!single_bm)
  132.         {
  133.                 error = "Couldn't allocate single height bitmap";
  134.                 goto done;
  135.         }
  136.  
  137.         DateStamp(&start_ds);
  138.  
  139.         /* creating the ripple effect */
  140.  
  141.         for(;;)
  142.         {
  143.                 BltBitMap(s->RastPort.BitMap, 0, (screen_h-(water_h*3))+i, double_bm, 0, i, screen_w, 1, 0xC0, 0xFF, NULL);
  144.  
  145.                 i=(i+1)%(water_h*2);
  146.  
  147.                 incv+=0.09;
  148.                 if(incv > (PID2 * 4)) incv=0;
  149.  
  150.                 inch+=0.06;
  151.                 if(inch > (PID2 * 4)) inch=0;
  152.  
  153.                 for (y = 0; y < water_h; y++)
  154.                 {
  155.                         p=(((double)(water_h-y))/((double)water_h));
  156.                         a=p*p*48+incv;
  157.  
  158.                         yoff=y+(int)(sin(a)*7)+1;
  159.                         yy=(water_h*2)-yoff;
  160.  
  161.                         aa=p*p*64+inch;
  162.                         off=(int)(sin(aa)*10*(1-p));
  163.  
  164.                         if(yy > 0 && yy < water_h*2)
  165.                         {
  166.                                 if(off > 0)
  167.                                         BltBitMap(double_bm, 0, yy, single_bm, off, y, screen_w - off, 1, 0xC0, 0xFF, NULL);
  168.                                 else
  169.                                         BltBitMap(double_bm, -off, yy, single_bm, 0, y, screen_w + off, 1, 0xC0, 0xFF, NULL);
  170.                         }
  171.                 }
  172.  
  173.                 BltBitMapRastPort(single_bm, 0, 0, w->RPort, 0, 0, screen_w, water_h, 0xC0);
  174.                 frames++;
  175.  
  176.                 if(!args[A_NOWAIT])
  177.                 {
  178.                         if(args[A_WAITTOF]) WaitTOF();
  179.                         else WaitBOVP(&s->ViewPort);
  180.                 }
  181.  
  182.                 if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) break;
  183.         }
  184.  
  185.         DateStamp(&end_ds);
  186.  
  187.         ticks = (end_ds.ds_Days-start_ds.ds_Days) * 86400*TICKS_PER_SECOND
  188.                         +(end_ds.ds_Minute-start_ds.ds_Minute) * 60*TICKS_PER_SECOND
  189.                         +(end_ds.ds_Tick-start_ds.ds_Tick);
  190.  
  191.         if(!args[A_QUIET]) printf("Frames per second: %ld\n", (TICKS_PER_SECOND * frames) / ticks+1);
  192.  
  193.         retval = RETURN_OK;
  194.  
  195. done:
  196.         if(error)     printf("%s\n", error);
  197.  
  198.         if(single_bm) FreeBitMap(single_bm);
  199.         if(double_bm) FreeBitMap(double_bm);
  200.         if(w)         CloseWindow(w);
  201.         if(s)         UnlockPubScreen(NULL, s);
  202.         if(rd)        FreeArgs(rd);
  203.  
  204.     DropInterface(IDOS);
  205.     DropInterface(IIntuition);
  206.     DropInterface(IGraphics);
  207.     CloseLibrary(IntuitionBase);
  208.     CloseLibrary(GfxBase);
  209.  
  210.         return(retval);
  211. }
  212.